package com.hou.shop.ware.service.impl;

import com.google.common.eventbus.DeadEvent;
import com.hou.common.constant.WareConstant;
import com.hou.shop.ware.controller.vo.MergeVo;
import com.hou.shop.ware.controller.vo.purchasefinishvo.ItemData;
import com.hou.shop.ware.controller.vo.purchasefinishvo.PurchaseFinishVo;
import com.hou.shop.ware.entity.PurchaseDetailEntity;
import com.hou.shop.ware.service.PurchaseDetailService;
import com.hou.shop.ware.service.WareSkuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.Query;

import com.hou.shop.ware.dao.PurchaseDao;
import com.hou.shop.ware.entity.PurchaseEntity;
import com.hou.shop.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    public PurchaseDetailService detailService;

    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }


    // 未领取采购单
    @Override
    public PageUtils queryUnreceivePage(Map<String, Object> params) {

        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", 0).or().eq("status", 1)
        );

        return new PageUtils(page);
    }

    // 合并采购单
    @Transactional
    @Override
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();

            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatus.CREARTE.getCode());

            this.save(purchaseEntity);
        }
        List<Long> items = mergeVo.getItems();
        List<PurchaseDetailEntity> collect = items.stream().map((obj) -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();

            detailEntity.setId(obj);
            detailEntity.setPurchaseId(purchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatus.ASSIGNED.getCode());

            return detailEntity;
        }).collect(Collectors.toList());

        detailService.updateBatchById(collect);

        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());

        this.updateById(purchaseEntity);
    }


    // 员工领取采购单
    @Override
    public void received(List<Long> purchaseIds) {

        // 1.确认采购单状态

        // 获取采购单信息
        List<PurchaseEntity> collect = purchaseIds.stream().map((obj) -> {
            PurchaseEntity purchaseEntity = this.getById(obj);
            return purchaseEntity;
        }).filter((obj) -> {
            if (obj.getStatus() == WareConstant.PurchaseStatus.CREARTE.getCode() ||
                    obj.getStatus() == WareConstant.PurchaseStatus.ASSIGNED.getCode()) {
                return true;
            }
            return false;
        }).map((obj) -> {
            obj.setStatus(WareConstant.PurchaseStatus.RECEIVE.getCode());
            obj.setUpdateTime(new Date());
            return obj;
        }).collect(Collectors.toList());

        // 2.改变采购单状态
        this.updateBatchById(collect);

        // 3.改变采购项的状态
        collect.forEach((item)->{
            // 获取采购项详细信息
            List<PurchaseDetailEntity> purchaseDetailEntities = detailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> changeData = purchaseDetailEntities.stream().map((obj) -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                BeanUtils.copyProperties(obj, purchaseDetailEntity);
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatus.BUYING.getCode());
                purchaseDetailEntity.setId(obj.getId());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            detailService.updateBatchById(changeData);
        });

    }

    @Override
    @Transactional
    public void done(PurchaseFinishVo purchaseFinishVo) {
        Long id = purchaseFinishVo.getId();

        // 2.改变采购项状态
        Boolean flag = true;
        List<ItemData> items = purchaseFinishVo.getItems();

        List<PurchaseDetailEntity> list = new ArrayList<>();
        for (ItemData item : items) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            // 采购失败
            if (item.getStatus() == WareConstant.PurchaseDetailStatus.ERR.getCode()) {
                flag = false;
                purchaseDetailEntity.setStatus(item.getStatus());
            } else {
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatus.FINISH.getCode());
                // 3.采购商品进行入库
                PurchaseDetailEntity byId = detailService.getById(item.getItemId());
                wareSkuService.addStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());
            }
            purchaseDetailEntity.setId(item.getItemId());
            list.add(purchaseDetailEntity);
        }

        detailService.updateBatchById(list);

        // 1.改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatus.FINISH.getCode() : WareConstant.PurchaseStatus.ERR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

    }

}